<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <meta name="author" content="彭俊超">
    <title>jq微博输入字数统计</title>
    <style>
        div{
            width:500px;
            height:300px;
            background-color:lightseagreen;
            margin:0 auto;
            padding:20px;
        }
        textarea{
            width:100%;
            height:200px;
            resize:none;
        }
        P{
            width:130px;
            height:50px;
            background-color:coral;
            margin:0 auto;
            margin-top:50px;
            border-radius:15px;
            line-height:50px;
            text-align: center;
        }
    </style>
</head>

<body>
<div>
    <textarea rows="" cols="" placeholder="请输入内容"></textarea>
    <p></p>
</div>

<script src="./jquery.js"></script>
</body>

</html>
<script>
var str = '0';
$('textarea').bind('input propertychange',function(){
    var count = 0;
    console.log('字符串长度'+$(this).val().length);
    str = $(this).val();
    console.log(str);
    // console.log(str.charCodeAt(0));
    for(var i = 0; i < $(this).val().length; i++){
        if( str.charCodeAt(i) < 255){
            count = count + 0.5;
        }else{
            count = count + 1;
        }              
    }
    // 字数超出进行改变字体的样式
    if(count > 30){
            $(this).css('color','red');
    }
    else{
            $(this).css('color','black');
    }
    
    console.log(count);
    $('p').html('输入字数='+Math.ceil(count));

})
  
</script>